package cn.jit.controller;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jit.bo.Page;
import cn.jit.po.User;
import cn.jit.service.UserService;

@Controller
public class TestPageController {
	
	@Autowired
	private UserService userService;

	
	
	@RequestMapping("/page/user/{startPage}/{eachPageNum}")
	@ResponseBody
	public Page<User> page1(@PathVariable("startPage") Integer startPage,
			@PathVariable("eachPageNum") Integer eachPageNum) {
		if (startPage == null) {
			startPage = 1;
		}
		
		if (eachPageNum == null) {
			eachPageNum = 5;
		}
		
		int totalCount = userService.count(new User());
		
		int pageNums = (int) Math.ceil(totalCount * 1.0 / eachPageNum);
		
		ArrayList<User> userList = userService.getUsers((startPage-1)*eachPageNum, eachPageNum);
		
		Page<User> page = new Page<User>();
		page.setWhichPage(startPage);
		page.setEachPageNum(eachPageNum);
		page.setTotalPages(pageNums);
		page.setTotalCount(totalCount);
		page.setCurrentPageCount(startPage, eachPageNum, pageNums, totalCount);
		page.setData(userList);
		System.out.println(userList);		
		return page;
	}
}
